from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field
from typing import Optional, Type
import requests

# 工单创建工具
class TicketInput(BaseModel):
    customer_id: str = Field(..., description="客户ID")
    issue: str = Field(..., description="问题描述")
    priority: Optional[str] = Field("medium", description="优先级: low, medium, high")


class CreateTicketTool(BaseTool):
    name = "create_ticket"
    description = "为客户问题创建支持工单"
    args_schema: Type[BaseModel] = TicketInput

    def _run(self, customer_id: str, issue: str, priority: str = "medium"):
        try:
            response = requests.post(
                "http://localhost:8000/api/tickets",
                json={
                    "customer_id": customer_id,
                    "issue": issue,
                    "priority": priority
                },
                timeout=5
            )
            return f"工单创建成功: {response.json()['ticket_id']}"
        except Exception as e:
            return f"工单创建失败: {str(e)}"